﻿using Avalonia.Collections;
using EcsDebugSimulator.Models;
using Microsoft.Extensions.DependencyInjection;
using Serilog;
using System.Threading.Tasks;
using System;
using XT.MNet.Tcp;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using EcsDebugSimulator.Services.SimulatorHandle;

namespace EcsDebugSimulator.ViewModels
{
    public partial class MainViewModel : ViewModelBase
    {
        [ObservableProperty]
        private AvaloniaList<string> _commandTypes;

        [ObservableProperty]
        private AvaloniaList<MsgLog> _logs;

        [ObservableProperty]
        private string _selectCommandType;

        [ObservableProperty]
        private AvaloniaList<string> _equipType;

        [ObservableProperty]
        private string _selectEquipType;

        [ObservableProperty]
        private CommandContentDto _contentDto;

        [ObservableProperty]
        private string _ip;

        [ObservableProperty]
        private string _port;

        [ObservableProperty]
        private string _equipCode;

        private TcpServer _tcpServer;


        [ObservableProperty]
        private string _sendText;

        private EquipSimulatorOperation equipSimulatorOperation;

        public MainViewModel()
        {
            ContentDto = new CommandContentDto();
            ContentDto.MachineGruop = new MachineGruopContent();
            ContentDto.MachineScreen = new MachineScreenContent();
            ContentDto.General = new GeneralContent();
            Logs = new AvaloniaList<MsgLog>();

            _equipType = new AvaloniaList<string>();
            _equipType.Add("机械手-组盘");
            _equipType.Add("机械手-筛选");
            _equipType.Add("机械手-拆盘");
            _equipType.Add("Ocv");
        }

        [RelayCommand]
        public async Task StartServer()
        {
            if (SelectEquipType!=null && Ip!=null && Port!=null)
            {
                equipSimulatorOperation = Register.App.GetService<EquipSimulatorOperation>();

                equipSimulatorOperation.Receive += Opera_Receive;

                equipSimulatorOperation.Send += Opera_Send;

                if (equipSimulatorOperation != null)
                {
                    var content = new CommandContent();
                    content.General = ContentDto.General;
                    content.MachineScreen = ContentDto.MachineScreen;
                    content.MachineGruop = ContentDto.MachineGruop;
                    content.MachineExcrete = ContentDto.MachineExcrete;
                    content.Ocv = ContentDto.Ocv;
                    equipSimulatorOperation.Init(Ip, int.Parse(Port), content, SelectEquipType);
                }
            }
        }

        private void Opera_Send(object? sender, string e)
        {
            Logs.Add(new MsgLog()
            {
                Content = e,
                SendDirection = "服务端",
                SendTime = DateTime.Now.ToString("F"),
            });
        }

        [RelayCommand]
        public async Task StopServer()
        {
            equipSimulatorOperation.OperationStop();
        }

        private void Opera_Receive(object? sender, string e)
        {
            Logs.Add(new MsgLog()
            {
                Content = e,
                SendDirection = "客户端",
                SendTime = DateTime.Now.ToString("F"),
            });
        }

        [RelayCommand]
        public async Task Send()
        {
            if (SendText!=null&& SendText.Length>0 && equipSimulatorOperation != null)
            {
                equipSimulatorOperation.WriteRequest(SendText);
            }
        }

        [RelayCommand]
        public async Task ClearLog()
        {
            if (Logs.Count > 0)
            {
                Logs.Clear();
                Logs = new AvaloniaList<MsgLog>();
            }
        }
    }
}
